using AutoMapper;
using CSRedis;
using SH.Common.CommonReturn;
using SH.Room.Application.IServices.IRoomType;
using SH.Room.Domain.RoomClass;
using SH.Room.Infrastructure.IRepository;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace SH.Room.Application.Services
{
    public class RoomTypeServices : BaseServices<RoomType>, IRoomTypeServices
    {
        private readonly IRoomTypeRepository _roomTypeRepository;
        private readonly IRoomNumRepository _roomNumRepository;

        private readonly IMapper _mapper;

        public RoomTypeServices(IRoomTypeRepository roomTypeRepository, IMapper mapper, IRoomNumRepository roomNumRepository, ISqlSugarClient _sqlSugarClient) : base(roomTypeRepository)
        {
            _roomTypeRepository = roomTypeRepository;
            _mapper = mapper;
            _roomNumRepository = roomNumRepository;
        }

        /// <summary>
        /// 房型下拉列表显示
        /// </summary>
        /// <returns>受影响行数</returns>
        public async Task<List<RoomTypeDto>> GetRoomList()
        {
            List<RoomType> roomTypes = await _roomTypeRepository.GetListAsync();
            List<RoomTypeDto> roomTypeDtos = _mapper.Map<List<RoomTypeDto>>(roomTypes);
            return roomTypeDtos;
        }

        /// <summary>
        /// 房型显示、查询、分页
        /// </summary>
        /// <param name="rtname">房型名称</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页容量</param>
        /// <returns>受影响行数</returns>
        public ApiPaging<List<RoomTypeListDto>> GetRoomTypeList(string? rtname, int pageIndex, int pageSize)
        {
            ApiPaging<List<RoomTypeListDto>> apiResult=new ApiPaging<List<RoomTypeListDto>>();
            var list = _roomTypeRepository.GetListByCondition(x => string.IsNullOrEmpty(rtname) || x.Rtname.Contains(rtname)).OrderByDescending(x => x.Rsort).Select(x => new RoomTypeListDto
            {
                Rtid = x.Rtid,
                Rtname = x.Rtname,
                Rprice = x.Rprice,
                Deposit = x.Deposit,
                RoomCount = x.RoomCount,
                Rsort = x.Rsort,
            });
            int totalCount = list.Count();
            int pageCount = (int) Math.Ceiling(totalCount*1.0/pageSize);
            var query = list.Skip((pageIndex-1)*pageSize).Take(pageSize).ToList();
            apiResult.TotalCount=totalCount;
            apiResult.PageCount=pageCount;
            apiResult.Data = query;
            return apiResult;
        }

        /// <summary>
        /// 房型添加
        /// </summary>
        /// <param name="roomType">房型添加</param>
        /// <returns>受影响行数</returns>
        public async Task<ApiResult<int>> RoomTypeAdd(RoomTypeAddDto roomTypeAddDto)
        {
            ApiResult<int> apiResult = new ApiResult<int>();
            var item = _roomTypeRepository.GetListByCondition(x => x.Rtname == roomTypeAddDto.Rtname).FirstOrDefault();
            if (item == null)
            {
                var room = _mapper.Map<RoomType>(roomTypeAddDto);
                room.TenantId = "zhangsan";
                room.RoomCount = 0;
                room.Rsort = 0;
                await _roomTypeRepository.InsertAsync(room);
                apiResult.Success();
            }
            else
            {
                apiResult.Error(ApiResult<int>.ErrorType.Request,"房型名称已有，请重新添加");
            }
            
            return apiResult;
        }


        /// <summary>
        /// 房型删除
        /// </summary>
        /// <param name="rtid">房型ID</param>
        /// <returns>受影响行数</returns>
        public ApiResult<int> DeleteRoomType(int rtid)
        {
            ApiResult<int> apiResult=new ApiResult<int>();
            var item = _roomNumRepository.GetListByCondition(x => x.Rtid == rtid).FirstOrDefault();
            if (item == null)
            {
                var info= _roomTypeRepository.DeleteRoomType(rtid);
                apiResult.Success();
            }
            else
            {
                apiResult.Error(ApiResult<int>.ErrorType.Request, "该房型下还有房号，删除房型前请先删除下属房号");
            }
            return apiResult;
            
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="roomType">房型</param>
        /// <returns>受影响行数</returns>
        public ApiResult<int> BatchDelRoomType(List<RoomType> roomType)
        {
            ApiResult<int> apiResult = new ApiResult<int>();
            foreach (var item in roomType)
            {
                var res = _roomTypeRepository.GetListByCondition(x => x.Rtid == item.Rtid).FirstOrDefault();
                if (res == null)
                {
                    var del = _roomTypeRepository.BatchDelete(roomType);
                    apiResult.Success(del);
                }
                else
                {
                    apiResult.Error(ApiResult<int>.ErrorType.Request, "该房型下还有房号，删除房型前请先删除下属房号");
                }
            }
            return apiResult;

        }

        /// <summary>
        /// 房型信息反填
        /// </summary>
        /// <param name="rtid">房型id</param>
        /// <returns>受影响行数</returns>
        public RoomType GetFindRoomType(int rtid)
        {
            var list = _roomTypeRepository.GetModelByCondition(x=>x.Rtid==rtid);
            return list;
        }

        /// <summary>
        /// 房型修改
        /// </summary>
        /// <param name="roomTypeUpdDto">房型修改DTO</param>
        /// <returns>受影响行数</returns>
        public ApiResult<int> UpdRoomType(RoomTypeUpdDto roomTypeUpdDto)
        {
            ApiResult<int> apiResult = new ApiResult<int>();
            var item = _mapper.Map<RoomType>(roomTypeUpdDto);
            var info= _roomTypeRepository.Upd(item);
            apiResult.Success(info);
            return apiResult;

        }

       
    }

}
